package com.y.fund.web.mem;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.y.fund.aacore.data.DataVo;
import com.y.fund.aacore.data.PaginationList;
import com.y.fund.aacore.enums.BoolVal;
import com.y.fund.aacore.vo.PagingVo;
import com.y.fund.domain.mem.MB13UserAccount;
import com.y.fund.domain.mem.MB13UserInfo;
import com.y.fund.domain.mem.MB15LoginRecord;
import com.y.fund.service.mem.UserService;
import com.y.fund.utils.IdGenerator;
import com.y.fund.utils.MD5Util;
import com.y.fund.utils.StrUtil;
import com.y.fund.web.BaseController;
import com.y.fund.web.aavo.com.IdsForceVo;
import com.y.fund.web.aavo.com.IdsOptVo;
import com.y.fund.web.aavo.mem.LoginRecordSearchVo;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@ApiSort(61)
@RestController
@RequestMapping(value="api/mem/user", produces = MediaType.APPLICATION_JSON_VALUE)
@Tag(name= "用户管理")
public class UserController extends BaseController{

	@Autowired
	protected UserService userService;
	
	/** 查询有用户坐标的用户行为列表 */

	@GetMapping(value = "list")
	@Operation(summary = "分页查询用户列表")
	@ApiOperationSupport(order = 1)
	public DataVo userList(MB13UserInfo userInfo, @RequestParam(name ="roleId", required = false) String roleId, PagingVo pvo) {
		PaginationList list=userService.findUserList(userInfo, roleId, pvo);
		return DataVo.Success(list);
	}
	
    @PostMapping("sv")
    @Operation(summary = "保存用户信息", description = "保存用户信息")
    @ApiOperationSupport(order = 4)
    public DataVo userSave(@RequestBody MB13UserInfo user) throws Exception{
    	if(StrUtil.isBlankIncNullStr(user.getLgnName())) {
    		return DataVo.Fail("用户登录名不能为空");
    	}
    	MB13UserInfo old = null;
    	if(StrUtil.NotBlank(user.getId())) {
    		old = commService.findObjById(MB13UserInfo.class, user.getId());
    	}
    	if(null != old) {
    		user.setId(old.getId());
    		user.setVersion(old.getVersion());
    		//登录名不能修改
			user.setLgnName(old.getLgnName());
			if(StrUtil.isBlankIncNullStr(user.getLgnPwd())) {
				user.setLgnPwd(old.getLgnPwd());
			}else {
		    	user.setLgnPwd(MD5Util.encode(user.getLgnPwd()));
			}
			commService.updtByIdIgnoreNull(user);
		}else {

	    	user.setId(IdGenerator.Id32());
	    	user.setEnabled(BoolVal.Yes.getVal());
	    	user.setBuiltin(BoolVal.No.getVal());
	    	
	    	
	    	MB13UserAccount uexp = new MB13UserAccount();
	    	uexp.setLgnName(user.getLgnName());
	    	if(commService.findSizeByExpCountById(uexp) > 0) {
	    		return DataVo.Fail("用户登录名已经存在");
	    	}
	    	if(StrUtil.isBlankIncNullStr(user.getLgnPwd())) {
	    		user.setLgnPwd("123456");
	    	}
	    	user.setLgnPwd(MD5Util.encode(user.getLgnPwd()));
	    	
	    	commService.addObj(user);
		}

    	return DataVo.Success(user.getId());
    }

	@PostMapping("updt")
	@Operation(summary = "修改人员信息", description = "仅修改非空字段")
	@ApiOperationSupport(order = 4)
	public DataVo updtStaffIngroalNull(@RequestBody MB13UserInfo user) throws Exception{
		if(StrUtil.isBlankIncNullStr(user.getId())) {
			return DataVo.FailNotData();
		}

		if(commService.updtByIdIgnoreNull(user)) {
			return DataVo.Success(user.getId());
		}else {
			return DataVo.Fail("save error");
		}
	}

	@GetMapping("detail/{id}")
	@Operation(summary = "用户详细信息")
	@ApiOperationSupport(order =7)
	public DataVo userDetail(@PathVariable(name="id") String id) throws Exception{
		if(StrUtil.isBlankIncNullStr(id)) {
			return DataVo.FailMustParam("id");
		}
		MB13UserInfo user = commService.findObjById(MB13UserInfo.class, id);
		LinkedHashMap<String, Object> data = new LinkedHashMap<String, Object>();
		data.put("dobj", user);
		return DataVo.Success(data);
	}

	@PostMapping(value = "del")
	@Operation(summary = "删除用户信息")
	@ApiOperationSupport(order = 33)
	public DataVo compDel(@RequestBody IdsForceVo idsForce) {
		if(null==idsForce.getFirstId()) {
			return DataVo.FailMustParam("id");
		}
		MB13UserInfo dobj = commService.findObjById(MB13UserInfo.class, idsForce.getFirstId());
		if(BoolVal.IsYes(dobj.getBuiltin())) {
			return DataVo.Fail("内置用户不可以删除");
		}
		if(commService.deleteEntity(dobj)) {
			return DataVo.Success();
		}else {
			return DataVo.FailDB();
		}
	}

	@GetMapping(value = "lgn/rcd/list")
	@Operation(summary = "分页查询用户登录日志列表")
	@ApiOperationSupport(order = 32)
	public DataVo lgnRecordList(LoginRecordSearchVo searchVo, PagingVo pvo) {
		PaginationList<MB15LoginRecord> list = userService.findLoginRecordList(searchVo, pvo);
		return DataVo.Success(list);
	}

	@PostMapping(value = "lgn/rcd/del")
	@Operation(summary = "批量删除用户登录日志")
	@ApiOperationSupport(order = 33)
	public DataVo lgnRecordList(@RequestBody IdsOptVo idsOpt) {
		if(null==idsOpt.getId() || idsOpt.getId().length<1) {
			return DataVo.FailMustParam("id");
		}

		List<MB15LoginRecord> dels = commService.findListByExpAndIdIn(new MB15LoginRecord(), Arrays.asList(idsOpt.getId()), null);
		if(commService.deleteEntitys(dels)) {
			return DataVo.Success();
		}else {
			return DataVo.FailDB();
		}
	}
}